package cn.fastoo.web.backstage.web.controller.member;

import cn.fastoo.common.dao.member.MemberDao;
import cn.fastoo.common.dao.member.MemberFeedbackDao;
import cn.fastoo.common.dao.member.MemberGuardianDao;
import cn.fastoo.common.entity.member.Member;
import cn.fastoo.common.entity.member.MemberFeedback;
import cn.fastoo.common.entity.member.MemberGuardian;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/member/guardian")
public class MemberGuardianController {


    @Autowired
    private MemberGuardianDao dao;

    @RequestMapping
    public String index() {
        return "/member/guardian/index";
    }


    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute MemberGuardian newData, QuerySearch querySearch, String op) {
         
        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                dao.insert(newData);
            }else {
                MemberGuardian oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    MemberGuardian memberGuardian = dao.findById(newData.getId());
                    newData.setGuardianName(memberGuardian.getGuardianName());
                    newData.setMobile(memberGuardian.getMobile());
                    dao.update(newData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(dao.findPage(querySearch.getSearch()),querySearch);
    }



    @RequestMapping("info")
    @ResponseBody
    public String info(@RequestParam Long id){
        return JsonResult.success(dao.findById(id));
    }

}
